<template>
	<view>
		<view class="headerPic">
			<u-avatar :src="src||require('@/static/harden2.jpg')"></u-avatar>
		</view>
		<view class="login_box">
			<u--form labelWidth='60' labelPosition="left" :model="model" :rules="rules" ref="form">
				<u-form-item label="手机号" prop="userInfo.phone" borderBottom>
					<u--input v-model="model.userInfo.phone" border="none"></u--input>
				</u-form-item>
				<u-form-item label="密码" prop="userInfo.password" borderBottom>
					<u--input v-model="model.userInfo.password" type='password' border="none"></u--input>
				</u-form-item>
			</u--form>
			<u-button type="error" @click='loginFn' text="确定"></u-button>
			<text @click="toRegister">没有账号？立即注册</text>
		</view>

	</view>
</template>

<script>
	import {
		loginAPI
	} from '@/api/api.js'
	export default {
		data() {
			return {
				model: {
					userInfo: {
						phone: '',
						password: ''
					},
				},
				rules: {
					'userInfo.phone': {
						type: 'string',
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					},
					'userInfo.password': {
						type: 'string',
						required: true,
						message: '请输入密码',
						trigger: ['blur', 'change']
					}
				},
				src: ''
			}
		},
		methods: {
			toRegister() {
				uni.navigateTo({
					url: '/pages/Register/Register'
				})
			},
			loginFn() {
				this.$refs.form.validate().then( async() => {
				 const res = await loginAPI(this.model.userInfo)
				 if(res.status===200){
					 this.$store.commit('sendToken',res.token)
					 uni.switchTab({
					 	url:'/pages/index/index'
					 })
				 }
				}).catch(errors => {
					uni.$u.toast('请输入正确的手机号和密码！')
				})
				
			}
		}
	}
</script>

<style scoped>
	.headerPic {
		margin-top: 100rpx;
		width: 750rpx;
		height: 300rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.login_box {
		width: 600rpx;
		margin: 0px auto;
	}

	.login_box>button {
		margin-top: 100rpx;
		margin-bottom: 20rpx;
	}

	.login_box>text {
		text-decoration: underline;
		color: yellowgreen;
	}
</style>
